package dao.impl;

import dao.BaseDao;
import dao.ElectrocarDao;
import entity.Powerbattery;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ElectrocarDaoImpl extends BaseDao implements ElectrocarDao {
    //查询全部
    @Override
    public List<Powerbattery> SelectAll() {
        List<Powerbattery> list = new ArrayList<>();
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select * from powerbattery");
            rs = pstmt.executeQuery();
            while (rs.next()){
                Powerbattery powerbattery = new Powerbattery();
                powerbattery.setId(rs.getInt("id"));
                powerbattery.setCarName(rs.getString("carName"));
                powerbattery.setCityName(rs.getString("cityName"));
                powerbattery.setUserName(rs.getString("userName"));
                powerbattery.setPresssureValue(rs.getInt("presssureValue"));
                powerbattery.setTime(rs.getString("time"));
                list.add(powerbattery);
            }
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    //删除
    @Override
    public int delete(int id) {
        int result = -1;
        try{
            conn = super.getConnection();
            pstmt = conn.prepareStatement("delete from powerbattery where id=?");
            pstmt.setInt(1,id);
            result = pstmt.executeUpdate();
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return result;
    }

    //修改
    @Override
    public int update(Powerbattery powerbattery) {
        int result=0;
        try{
            conn=super.getConnection();
            pstmt = conn.prepareStatement("UPDATE `powerbattery` SET `carName` = ?, `cityName` = ?, `userName` = ?, `presssureValue` = ?, `time` = ? WHERE `id` = ?");
            pstmt.setString(1,powerbattery.getCarName());
            pstmt.setString(2,powerbattery.getCityName());
            pstmt.setString(3, powerbattery.getUserName());
            pstmt.setInt(4,powerbattery.getPresssureValue());
            pstmt.setString(5,powerbattery.getTime());
            pstmt.setInt(6,powerbattery.getId());
            result = pstmt.executeUpdate();
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return result;
    }

    //详细
    @Override
    public Powerbattery detail(int id) {
        Powerbattery powerbattery = new Powerbattery();
        try{
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select `id`, `carName`, `cityName`, `userName`, `presssureValue`, `time` from powerbattery where id=?");
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            if (rs.next()){
                powerbattery.setId(rs.getInt(1));
                powerbattery.setCarName(rs.getString(2));
                powerbattery.setCityName(rs.getString(3));
                powerbattery.setUserName(rs.getString(4));
                powerbattery.setPresssureValue(rs.getInt(5));
                powerbattery.setTime(rs.getString(6));
            }
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return powerbattery;
    }
}
